import { createSlice, PayloadAction } from '@reduxjs/toolkit'

import type {Tab} from 'rc-tabs/es/interface'
import type { ToolsItemType } from '@/components/ToolsGroup';


// 为 slice state 定义一个类型
interface OpDrawerState {
    tabItems: ToolsItemType[]
}
const initialState: OpDrawerState = {
    tabItems: []
}

export const opDrawerSlice = createSlice({
    name: 'opDrawer',
    initialState,
    reducers: {
        addTab: (state, action: PayloadAction<ToolsItemType>) => {
            if (!state.tabItems.find(item => item.code === action.payload.code)) {
                state.tabItems.push(action.payload)
            }
        },
        delTab: (state, action: PayloadAction<string>) => {
            state.tabItems = state.tabItems.filter(item => item.code !== action.payload)
        }
    }
})

export type TabItem = Tab

export const { addTab, delTab } = opDrawerSlice.actions

export default opDrawerSlice.reducer